package com.huice.day11;

import java.util.HashMap;
import java.util.Map;

import com.huice.Interface.Md5Tool;
import com.huice.base.CheckPoint;


public class Template {

	protected static String STATUS = "status";
	protected static String RESPONSE = "response";
	protected static String TIME = "time";
	protected static String METHOD = "method";
	protected static String PARA = "para";
	protected static String USER = "C100000362";
	protected static String PWD = "4a071cd838d7fbeec1225dd7d125be3a";
	protected static String HOST = "api.lixing.biz/service";
	protected static Map<String, String> environment = new HashMap<String, String>();
	
	protected Map<String, String> paras = new HashMap<String, String>();
	protected Map<String, String> headers = new HashMap<String, String>();
	protected Map<String, String> response = new HashMap<String, String>();
	protected CheckPoint checkPoint = new CheckPoint("", this.getClass().getSimpleName());
	
	
	public void clearEnvironment(){
		environment.clear();
	}
	
	public void setEnvironment(String key, String value){
		environment.put(key, value);
	}
	
	public String getEnvironment(String key){
		return environment.get(key);
	}
	
	protected void setPara(String key, String value){
		paras.put(key, value);
	}
	
	protected void setHeader(String key, String value){
		headers.put(key, value);
	}
	
	protected void send(){
		if(paras.containsKey("para")){
			paras.put("sign", getSign(paras.get("para"),PWD));
		}else{
			paras.put("sign", getSign(null,PWD));
		}
		
		if(!paras.containsKey("user")){
			paras.put("user", USER);
		}

		response = HttpUtil.doPostByURL(HOST, headers, paras);
	}
	
	protected String getStatus(){
		return response.get(STATUS);
	}
	
	protected String getTime(){
		return response.get(TIME);
	}
	
	protected String getResponse(){
		return response.get(RESPONSE);
	}
	
	protected XmlStringParser xmlCheck(){
		return new XmlStringParser(getResponse());
	}
	
	private String getSign(String para, String key){
		String source = null;
		if(para!=null){
			source = "para"+para+key;
		}else{
			source = key;
		}
		return Md5Tool.getInstance().getLongToken(source, "UTF-8");
	}
}
